<%@page import="system.Pagination"%>
<%@page import="java.util.ArrayList" %>
<%@page import="models.Kategori"%>
<%@page import="models.Barang"%>

<% 
ArrayList<Kategori> listKategori = (ArrayList<Kategori>) request.getAttribute("listKategori");
ArrayList<Barang> listBarang = (ArrayList<Barang>) request.getAttribute("listBarang");

// Pagination
// Current page
int currentPage = 0;
try {
    currentPage = Integer.parseInt(request.getParameter("page"));
} catch (Exception e) {
}

// Kategori Id
Integer kategoriId = null;
try {
    kategoriId = Integer.parseInt(request.getParameter("kategori_id"));
} catch (Exception e) {
}

// Total item
int totalItem = (Integer) request.getAttribute("totalItem");

// Item per Page
int itemPerPage = (Integer)request.getAttribute("itemPerPage");

// Keywords
String keywords = request.getParameter("keywords");
if(keywords == null) {
    keywords = "";
}

// Url
String url = request.getContextPath() + "/site/index?";
if(keywords != null && kategoriId != null) {
    url += "kategori_id=" + kategoriId + "&";
}
if(!"".equals(keywords)) {
    url += "keywords=" + keywords + "&";
}
%>

<h1>Home</h1>
<div class="main-wrapper">
    <div id="main-leftside">
        <div id="leftside-title">
            Kategori
        </div>
        <div id="leftside-container">
            <a href="<%=request.getContextPath()%>/site/index">
                <div class="category" >
                    Semua
                </div>
            </a>
            <% if(listKategori != null) for (int i = 0; i < listKategori.size(); i++) {%>
            <a href="<%=request.getContextPath()%>/site/index?kategori_id=<%=listKategori.get(i).id%>">
                <div class="category" >
                    <%=listKategori.get(i).nama%>
                </div>
            </a>
            <% }%>
        </div>
    </div>
    <div id="main-rightside">
        <div id="rightside-search">
            <script type="text/javascript">
                function removeText() {
                    document.getElementById('keywords').value = "";
                }
            </script>
            <form action="<%=request.getContextPath() + "/site/index" + (kategoriId != null ? "?kategori_id=" + kategoriId : "")%>" method="GET">
                <%if(kategoriId != null) {%>
                <input type="hidden" name="kategori_id" value="<%=kategoriId%>" />
                <%}%>
                <input type="text" name="keywords" id="keywords" value="<%=keywords%>" />
                <input type="submit" value="go" id="go-search" />
            </form>
        </div>
        <div id="rightside-container">
            <div class="item-container" id="main-item" style="display: inline">
                <% if(listBarang != null) for (int i = 0; i < listBarang.size(); i++) {%>
                <div class="item-barang" style="display: inline">
                    <div>
                        <a href="<%=request.getContextPath()%>/site/barang?barang_id=<%=listBarang.get(i).id%>">
                            <img class="item-image" src="<%= request.getContextPath() + "/" + (listBarang.get(i) != null ? listBarang.get(i).path_gambar : "")%>" alt=""/>
                        </a>
                    </div>
                    <div class="item">
                        <a href="<%=request.getContextPath()%>/barang/view?id=<%=listBarang.get(i).id%>">
                            <%=listBarang.get(i).nama%>
                        </a>
                    </div>
                    <div class="item">
                        <%=listBarang.get(i).harga_satuan%>
                    </div>
                    <div class="item">
                        <%if(listBarang.get(i).jumlah < 1){%>
                        <div class="no-cart"></div>
                        <%} else {%>
                        <form action="<%=request.getContextPath()%>/site/cart/addItem" method="POST">
                            <input name="itemID" type="hidden" value="<%= listBarang.get(i) != null ? listBarang.get(i).id : ""%>">
                            <input type="submit" class ="cart"<%if(listBarang.get(i).jumlah < 1){%>disabled="disabled"<%}%> value="Tambahkan ke keranjang!" />
                        </form>
                        <%}%>
                    </div>
                </div>
                <% }%>
            </div>
            <div class="clear"></div>
            <% if(listBarang != null && listBarang.size() > 0) { %>
            <div class="pager">
                <%= Pagination.getInstance("pagination", url, currentPage, totalItem, itemPerPage).run() %>
            </div>
            <% } %>
        </div>
    </div>
    <div class="clear"></div>
</div>